package 人事管理系统.Connect;

import 人事管理系统.Tables.Department;
import 人事管理系统.Tables.Employee;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.ArrayList;

public class Department_Database {

    private  String ip=this.getIp();
    private String url="jdbc:mysql://"+ip+":3306/人事管理系统?useOldAliasMetadataBehavior=true";
    //获取ip
    public String getIp(){
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: "+hostname);
        return addr.getHostAddress();
    }

    /**********************************************************************************************************************/

    /*
   查询所有部门信息
    */
    public ArrayList<Department> findAll()   {
        ArrayList<Department> list=new ArrayList<>();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM departmentinfo ";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){
                //封装对象
                Department dd=new Department();
                dd.setDno(rs.getString("DNO"));
                dd.setDname(rs.getString("DNAME"));
                dd.setManager(rs.getString("manager"));
                list.add(dd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return list;
    }
/**********************************************************************************************************************/
 /*
    条件查询，根据Dno查询部门信息
     */
    public  Department findByDno(String Dno){
        Department dd=new Department();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM departmentinfo WHERE DNO= '"+Dno+"'";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){
                dd.setDno(rs.getString("DNO"));
                dd.setDname(rs.getString("DNAME"));
                dd.setManager(rs.getString("manager"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return dd;
    }
/**********************************************************************************************************************/
   /*
    插入一条部门信息
     */
    public  int insert(Department dep){

        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="call Insert_depar('"+dep.getDno()+"','"+dep.getDname()+"','"+dep.getManager()+"')";
            result=stat.executeUpdate(sql);
            //6.处理结果

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            //new opyes();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }


/**********************************************************************************************************************/
   /*
    删除部门信息
     */
    public  int delete(String Dno){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="call Delete_depar('"+Dno+"')";
            result=stat.executeUpdate(sql);
            //6.处理结果
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//        //成功弹窗
//        if(result>0)
//            new opyes();
//        else new opno();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /*
修改部门信息
 */
    public  int updata(Department gds){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String sql="UPDATE departmentinfo SET DNO='"+gds.getDno()+"',DNAME='"+gds.getDname()+"',manager='"+gds.getManager()+"'WHERE DNO='"+gds.getDno()+"'";
            result=stat.executeUpdate(sql);
            //6.处理结果
            //   '"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"'
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            if (result>0)
                new opyes();
            else new opno();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }
    //操作成功弹窗
    class opyes{
        public opyes(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("操作成功！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });


        }
    }
    //操作失败弹窗
    class opno{
        public opno(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("没有该项数据！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });}}
}
